#include "CircleEmitter.h"
#include "ParticleFlyweight.h"

CCircleEmitter::CCircleEmitter(void)
{

}


CCircleEmitter::~CCircleEmitter(void)
{

}

void CCircleEmitter::SpawnParticles(int maxParticles,
									float minPosX, float maxPosX, float minPosY, float maxPosY)
{
	//if( maxParticles > 0)
	//	for(int i = 0; i <= maxParticles; ++i)
	//	{
	//		float Life =(float)(rand()%((int)m_pFLW->MaxLife() - (int)m_pFLW->MinLife())+(int)m_pFLW->MinLife());

	//		float posX, posY;

	//		//float diameter = this->Width();
	//		//float radius = diameter/2.0f;
	//		//float pi = 3.14;
	//		//float area = 2*pi*radius;

	//		//float centerX = this->PosX() + this->Width()/2;
	//		//float centerY = this->PosY() + this->Height()/2;

	//		//bool run = true;
	//		//while(run)
	//		//{
	//		//	float xdistance1 = 0, xdistance2 = 0, ydistance1 = 0, ydistance2 = 0;
	//		//	posX =(float)(rand()%((int)maxPosX - (int)minPosX)+(int)minPosX);
	//		//	posY =(float)(rand()%((int)maxPosY - (int)minPosY)+(int)minPosY);

	//		//	//distance = sqrt(((x2-x1)*(x2-X1))+((y2-y1)*(y2-y1)))
	//		//	xdistance1 = sqrt(((posX-centerX)*(posX-centerX))+((posY-centerY)*(posY-centerY)));
	//		//	if(xdistance1 <= radius && ydistance1 <= radius)
	//		//	{
	//		//		run = false;
	//		//	}
	//		//}
	//		m_LiveParticles.push_back(CParticle(Life, posX, posY,m_pFLW));
	//	}

	if( maxParticles > 0)
		for(int i = 0; i <= maxParticles; ++i)
		{
			//float Life =(float)(rand()%((int)m_pFLW->MaxLife() - (int)m_pFLW->MinLife())+(int)m_pFLW->MinLife());
			float Life = CalcLifetime();

			float posX, posY;

			CalcEmitRange( minPosX, maxPosX, minPosY, maxPosY, posX, posY);

			if(m_DeadParticles.size() == 0)
			{
				m_LiveParticles.push_back(CParticle(Life, posX, posY,m_pFLW));
			}
			else if(m_DeadParticles.size() > 0 && m_bIsLooping == true)
			{
				CParticle temp = *(m_DeadParticles.begin());
				m_DeadParticles.pop_front();
				temp.InitializeParticle(Life, posX, posY,m_pFLW);
				m_LiveParticles.push_back(temp);
			}
		}

}
void CCircleEmitter::CalcEmitRange(float& minPosX, float& maxPosX, float& minPosY, float& maxPosY, float& endX, float& endY)
{


	float diameter = this->Width();
	float radius = diameter/2.0f;
	float pi = 3.14f;
	float area = 2*pi*radius;

	float centerX = this->PosX() + this->Width()/2;
	float centerY = this->PosY() + this->Height()/2;

	bool run = true;
	while(run)
	{
		float xdistance1 = 0, xdistance2 = 0, ydistance1 = 0, ydistance2 = 0;
		endX =(float)(rand()%((int)maxPosX - (int)minPosX)+(int)minPosX);
		endY =(float)(rand()%((int)maxPosY - (int)minPosY)+(int)minPosY);

		//distance = sqrt(((x2-x1)*(x2-X1))+((y2-y1)*(y2-y1)))
		xdistance1 = sqrt(((endX-centerX)*(endX-centerX))+((endY-centerY)*(endY-centerY)));
		if(xdistance1 <= radius && ydistance1 <= radius)
		{
			run = false;
		}
	}
}